package cn.lg.soar.database.util;

import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luguoxiang
 * @date 2022/6/3
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class TableInfoPlusHelper extends TableInfoHelper {

    /**
     * 获取 property:column 映射
     * @param entityClass
     * @return
     */
    public static Map<String, String> getMapPropertyColumn(Class<?> entityClass) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
        List<TableFieldInfo> fieldList = tableInfo.getFieldList();
        Map<String, String> map = new HashMap<>(fieldList.size() + 1);
        fieldList.forEach(x -> {
            map.put(x.getProperty(), x.getColumn());
        });
        map.put(tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
        return map;
    }

    /**
     * 获取 column:property 映射
     * @param entityClass
     * @return
     */
    public static Map<String, String> getMapColumnProperty(Class<?> entityClass) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
        List<TableFieldInfo> fieldList = tableInfo.getFieldList();
        Map<String, String> map = new HashMap<>(fieldList.size() + 1);
        fieldList.forEach(x -> {
            map.put(x.getColumn(), x.getProperty());
        });
        map.put(tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
        return map;
    }

}
